//
//  ReviewsViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/11/14.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SwiftyJSON
import SVProgressHUD


class ReviewsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    
    var productID : String?
    
    
    var myTableView = UITableView(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64), style: .plain)
    
    var dataArray = NSMutableArray()
    
    let REVIEWTABLEVIEWCELL = "ReviewTableviewCell"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.creatNavBtn()
        
        self.creatTableView()
        self.getData()
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ReviewsViewController{
    fileprivate func creatNavBtn(){
        
        self.title = "REVIEWS"
        self.automaticallyAdjustsScrollViewInsets = false
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "nav_back"), for: UIControlState())
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
        
        
        let editItemCustomeView   = UIButton(type: UIButtonType.custom);
        editItemCustomeView.frame = CGRect(x: 0, y: 0, width: 30, height: 30);
        editItemCustomeView.setImage(UIImage(named:"nav_edit"), for: UIControlState())
        editItemCustomeView.addTarget(self, action: #selector(editBtnClick), for: UIControlEvents.touchUpInside)
        let editBarButtonItem = UIBarButtonItem(customView: editItemCustomeView)
        
        
        let rightItemCustomeView   = UIButton(type: UIButtonType.custom);
        rightItemCustomeView.frame = CGRect(x: 0, y: 0, width: 30, height: 30);
        rightItemCustomeView.setImage(UIImage(named:"nav_close"), for: UIControlState())
        rightItemCustomeView.addTarget(self, action: #selector(rightButtonClick), for: UIControlEvents.touchUpInside)
        let colseBarButtonItem = UIBarButtonItem(customView: rightItemCustomeView)
        
        
        self.navigationItem.rightBarButtonItems = [colseBarButtonItem,editBarButtonItem];
        
        
    }
    
    
    
    func leftBtnClick(){
        self.navigationController?.popViewController(animated: true)
    }
    
    func editBtnClick(){
        
        let editVC = EditViewController()
        editVC.productID = self.productID
        self.navigationController?.pushViewController(editVC, animated: true)
        
        
    }
    
    func rightButtonClick()
    {

        self.navigationController?.popToRootViewController(animated: true);
    }
    
    
}


extension ReviewsViewController{
    
    fileprivate func getData(){
        print(String(describing: self.productID!))
        let parma = ["main_page":"product_reviews","products_id":String(describing: self.productID!)]
        NetworkTool.shareInstance.request(.get, urlString: APIBASE, parmas: parma as [String : NSObject] ) { (result, error) in
            
            if result != nil{
                let json = JSON(result!)
                print(json)
                
                let dic = result as? NSDictionary
                let dataDic = dic!["data"] as? NSDictionary
                let reviewsArray = dataDic?["reviews_list"] as? NSArray
//                print(reviewsArray?.count)
//                print((reviewsArray?.count)!)
                if reviewsArray?.count != nil{
                    for dic in reviewsArray!{
                        
                        let model = ReviewModel(dictionary: dic as! NSDictionary)
                        
                        self.dataArray.add(model)
                        
                    }
                }
                
                
                
                self.myTableView.reloadData()
                
            }else{
                SVProgressHUD.showError(withStatus: "Commodity info error:" + String(describing: error))
                print("Commodity info error:" + String(describing: error))
            }
            
            
            
        }
        
        
        
    }
    
    
}

extension ReviewsViewController{
    
    fileprivate func creatTableView(){
        
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        
        let nib = UINib(nibName: "ReviewTableViewCell", bundle: nil)
        myTableView.register(nib, forCellReuseIdentifier: REVIEWTABLEVIEWCELL)
        
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: REVIEWTABLEVIEWCELL) as! ReviewTableViewCell
        cell.selectionStyle = .none
        let model = self.dataArray[indexPath.row] as! ReviewModel
        
        cell.model = model
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SCREEN_HEIGHT * 0.25
    }
    
}







